package com.eight.cloud.payment.core;

import cn.hutool.core.util.ObjectUtil;
import com.eight.cloud.core.global.exception.ParameterException;
import com.eight.cloud.payment.common.enums.PayStrategyEnum;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @ClassName: PaymentFactory
 * @Author: TXC
 * @Date: 2024-10-22 15:59
 **/
@Component
public class PaymentFactory {
    private final Map<PayStrategyEnum, IPaymentStrategy> strategyMap = new ConcurrentHashMap<>();

    public PaymentFactory(List<IPaymentStrategy> strategies) {
        for (IPaymentStrategy strategy : strategies) {
            strategyMap.put(strategy.getPayStrategy(), strategy);
        }
    }

    public IPaymentStrategy getPaymentService(PayStrategyEnum payStrategyEnum) {
        IPaymentStrategy service = strategyMap.get(Objects.requireNonNull(payStrategyEnum));
        if (ObjectUtil.isNotNull(service)) {
            return service;
        }
        throw new ParameterException("不支持的支付方式: " + payStrategyEnum.getChannel() + "-" + payStrategyEnum.getType());
    }
}
